﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using Tesla.Math;

namespace Tesla.Content {
    /// <summary>
    /// Parameter class for image loading.
    /// </summary>
    public class ImageLoaderParameters : LoaderParameters {
        private bool m_flip;
        private bool m_genMipMaps;
        private bool m_resizePowerOfTwo;
        private TextureConversionFormat m_textureFormat;
        private Color m_colorKey;
        private bool m_colorKeyEnabled;
        private bool m_preMultiplyAlpha;

        /// <summary>
        /// Gets or sets if images should be flipped when they are loaded. Default is false.
        /// </summary>
        public bool FlipImage {
            get {
                return m_flip;
            }
            set {
                m_flip = value;
            }
        }

        /// <summary>
        /// Gets or sets if mip maps should be generated for textures that do not have them.
        /// Existing mip maps are no changed. Default is true.
        /// </summary>
        public bool GenerateMipMaps {
            get {
                return m_genMipMaps;
            }
            set {
                m_genMipMaps = value;
            }
        }

        /// <summary>
        /// Gets or sets if the texture should be resized to the next largest power of two
        /// dimension for compatibility. Default is false.
        /// </summary>
        public bool ResizePowerOfTwo {
            get {
                return m_resizePowerOfTwo;
            }
            set {
                m_resizePowerOfTwo = value;
            }
        }

        /// <summary>
        /// Gets or sets what format the texture should be converted to upon import. If NoChange, then
        /// the image's default format is used if possible.
        /// </summary>
        public TextureConversionFormat TextureFormat {
            get {
                return m_textureFormat;
            }
            set {
                m_textureFormat = value;
            }
        }

        /// <summary>
        /// Gets or sets the color key. Any pixel that matches this color will get replaced by transparent black.
        /// </summary>
        public Color ColorKey {
            get {
                return m_colorKey;
            }
            set {
                m_colorKey = value;
            }
        }

        /// <summary>
        /// Gets or sets if the color key should be enabled.
        /// </summary>
        public bool ColorKeyEnabled {
            get {
                return m_colorKeyEnabled;
            }
            set {
                m_colorKeyEnabled = value;
            }
        }

        /// <summary>
        /// Gets or sets if the texture should be converted to a pre-multiplied alpha format.
        /// </summary>
        public bool PreMultiplyAlpha {
            get {
                return m_preMultiplyAlpha;
            }
            set {
                m_preMultiplyAlpha = value;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="ImageLoaderParameters"/>, with default parameters.
        /// </summary>
        public ImageLoaderParameters() {
            m_flip = false;
            m_genMipMaps = true;
            m_resizePowerOfTwo = false;
            m_textureFormat = TextureConversionFormat.NoChange;
            m_colorKey = new Color(255, 0, 255, 255);
            m_colorKeyEnabled = false;
            m_preMultiplyAlpha = false;
        }

        /// <summary>
        /// Creates a new instance of <see cref="ImageLoaderParameters"/>.
        /// </summary>
        /// <param name="flipImage">if set to <c>true</c> [flip image].</param>
        /// <param name="genMipMaps">if set to <c>true</c> [gen mip maps].</param>
        /// <param name="resizePowerTwo">if set to <c>true</c> [resize power two].</param>
        public ImageLoaderParameters(bool flipImage, bool genMipMaps, bool resizePowerTwo) {
            m_flip = flipImage;
            m_genMipMaps = genMipMaps;
            m_resizePowerOfTwo = resizePowerTwo;
            m_textureFormat = Content.TextureConversionFormat.NoChange;
            m_colorKey = new Color(255, 0, 255, 255);
            m_colorKeyEnabled = false;
            m_preMultiplyAlpha = false;
        }

        /// <summary>
        /// Serializes the object and writes it to the output.
        /// </summary>
        /// <param name="output">Savable Output</param>
        public override void Write(ISavableWriter output) {
            base.Write(output);
            output.Write("FlipImage", m_flip);
            output.Write("GenerateMipMaps", m_genMipMaps);
            output.Write("ResizePowerOfTwo", m_resizePowerOfTwo);
            output.WriteEnum<TextureConversionFormat>("TextureFormat", m_textureFormat);
            output.Write("ColorKey", m_colorKey);
            output.Write("ColorKeyEnabled", m_colorKeyEnabled);
            output.Write("PreMultiplyAlpha", m_preMultiplyAlpha);
        }

        /// <summary>
        /// Deserializes the object and populates it from the input.
        /// </summary>
        /// <param name="input">Savable input</param>
        public override void Read(ISavableReader input) {
            base.Read(input);
            m_genMipMaps = input.ReadBoolean();
            m_genMipMaps = input.ReadBoolean();
            m_resizePowerOfTwo = input.ReadBoolean();
            m_textureFormat = input.ReadEnum<TextureConversionFormat>();
            m_colorKey = input.ReadColor();
            m_colorKeyEnabled = input.ReadBoolean();
            m_preMultiplyAlpha = input.ReadBoolean();
        }
    }
}
